package com.wish.algorithm;

/**
 * Created by suliucheng on 2021/4/6 21:55
 * Directions: 求 1！+2！+...+10!
 * 1  2  6  24  120  720
 * 1  3  9  33  153  873
 */
public class MathDemoFactorial {
    public static void main(String[] args) {
        int num = 10;
        int result = getFactorialSum(num);
        System.out.println(result);
    }

    private static int getFactorialSum(int num) {
        int sum = 0;
        for (int i = 1; i <= num; i++) {
            sum += factorial(i);
        }
        return sum;
    }

    /**
     * 求阶乘 i!
     *
     * @param i i！
     * @return i！
     */
    private static int factorial(int i) {
        while (true) {
            if (i == 1) {
                return 1;
            }
            int result = i * factorial(i - 1);
            i--;
            return result;
        }

    }

}
